﻿using K9Nano.Dependency;

namespace K9Nano.Caching;

public class DefaultTypedCacheCleanerFactory : ITypedCacheCleanerFactory, ISingletonDependency
{
    private readonly IServiceProvider _serviceProvider;

    public DefaultTypedCacheCleanerFactory(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public ITypedCacheCleaner Create(Type cachedValueType)
    {
        var cacheType = typeof(ITypedCache<>).MakeGenericType(cachedValueType);
        return (ITypedCacheCleaner)_serviceProvider.GetService(cacheType)!;
    }

    public ITypedCacheCleaner Create<TCachedValue>() where TCachedValue : class
    {
        return (ITypedCacheCleaner)_serviceProvider.GetService(typeof(ITypedCache<TCachedValue>))!;
    }
}